package com.liy.framework.common.websocket.rest;

import com.liy.framework.common.extvo.ResultVO;
import com.liy.framework.common.webmsg.WebMessage;
import com.liy.framework.common.websocket.server.WebMessageUtil;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/webMessage"})
public class WebMessageRest {

    @ResponseBody
    @RequestMapping({"/sendClient/{clientId}"})
    public ResultVO sendClient(@RequestBody WebMessage message, @PathVariable("clientId") String clientId) {
        ResultVO resultVO = new ResultVO();
        String result = WebMessageUtil.sendToClient(clientId, message);
        resultVO.setMessage(result);
        resultVO.setSuccess(WebMessageUtil.SUCCESS.equals(result));
        return resultVO;
    }

    @ResponseBody
    @RequestMapping({"/sendGroup/{groupId}"})
    public ResultVO sendGroup(@RequestBody WebMessage message, @PathVariable("groupId") String groupId) {
        ResultVO resultVO = new ResultVO();
        String result = WebMessageUtil.sendToGroup(groupId, message);
        resultVO.setMessage(result);
        resultVO.setSuccess(WebMessageUtil.SUCCESS.equals(result));
        return resultVO;
    }

    @ResponseBody
    @RequestMapping({"/sendRole/{roleId}"})
    public ResultVO sendRole(@RequestBody WebMessage message, @PathVariable("roleId") String roleId) {
        ResultVO resultVO = new ResultVO();
        String result = WebMessageUtil.sendToRole(roleId, message);
        resultVO.setMessage(result);
        resultVO.setSuccess(WebMessageUtil.SUCCESS.equals(result));
        return resultVO;
    }
}
